﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Portfolio.Entities;
using Portfolio.Repository.Common;
using Portfolio.Repository.Interfaces;

namespace Portfolio.Repository.Articles
{
    public class ArticlesRepository : GenericRepository, IArticlesRepository
    {
        /// <summary>
        /// Konstruktor dziedziczy z klasy Generycznej (ogólnej) Repozytorium, domyślny context=Portfolio3Entities
        /// </summary>
        /// <param name="context"></param>
        public ArticlesRepository() : base() { }

        public ArticlesRepository(ObjectContext context) : base(context) { }        

        public ArtilcesGridV GetArticleByIdArticle(int articleID)
        {
            return Single<ArtilcesGridV>(a => a.ArticleID == articleID);                      
        }

        public List<ArticleT> GetArticlesByArticleCategoriesID(int pArticleCategoryID)
        {            
            return Find<ArticleT>(a => a.ArticleCategoryID == pArticleCategoryID).ToList();            
        }       

        public List<ArtilcesGridV> GetArticleBySearch(string title, int? categoryID, int? status, bool? active)
        {
           if (categoryID == null && status == null && active == null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title)).ToList();
           }
           else
           if (categoryID == null && status == null && active != null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ActiveFlag == active).ToList();
           }
           else
           if (categoryID == null && status != null && active == null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.Status == status).ToList();
           }
           else
           if (categoryID != null && status == null && active == null)
           {
                return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ArticleCategoryID == categoryID).ToList();
           }
           else
           if (categoryID == null && status != null && active != null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ActiveFlag == active && a.Status == status).ToList();
           }           
           else
           if (categoryID != null && status == null && active != null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ArticleCategoryID == categoryID && a.ActiveFlag == active).ToList();
           }else
           if (categoryID != null && status != null && active == null)
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ArticleCategoryID == categoryID && a.Status == status).ToList();
           }
           else
           {
               return Find<ArtilcesGridV>(a => a.Title.Contains(title) && a.ArticleCategoryID == categoryID && a.ActiveFlag == active && a.Status == status).ToList();
           }
            
            //return Find<ArtilcesGridV>(a => a.Title.Contains(title) && (((categoryID != null) && (a.ArticleCategoryID == categoryID)) || ((categoryID == null))) && (((status != null) && (a.Status == status))||((status == null))) && (((active != null) && (a.ActiveFlag == active))||((active != null))) ).ToList();
        }

        public void UpdateArticle(ArticleT Article)
        {
            Update<ArticleT>(Article); 
        }


        public void DeleteArticle(ArticleT article)
        {           
            Delete(article);
        }

        public int InsertArticle(ArticleT article)
        {
            return Add(article);
        }      

        
    }
}
